package com.wxy.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**rabbitMQ死信队列的几种情况
 * 1.ttl
 * 2.
 * @author 47868
 */
@Configuration
public class RabbitmqConfig {
    private static final String QUEUE_NAME = "wxy.queue";
    private static final String EXCHANGE_NAME = "wxy.exchange";
    private static final String ROUTING_KEY = "wxyKey";

    private static final String DEAD_EXCHANGE = "dead.exchange";
    private static final String DEAD_QUEUE = "dead.queue";
    private static final String DEAD_ROUTING_KEY = "deadKey";

    @Bean
    public Queue queue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        args.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY);
        return QueueBuilder.durable(QUEUE_NAME)
                .withArguments(args)
                //.maxLength(3)
                .ttl(500)
                .build();
    }
    @Bean
    public DirectExchange exchange() {
        return ExchangeBuilder.directExchange(EXCHANGE_NAME).durable(true).build();
    }
    @Bean
    public Binding binding(@Autowired DirectExchange exchange ,@Autowired Queue queue) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(ROUTING_KEY);
    }

    @Bean
    public Queue deadQueue() {
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }
    @Bean
    public Exchange deadExchange() {
        return ExchangeBuilder.directExchange(DEAD_EXCHANGE).durable(true).build();
    }
    @Bean
    public Binding deadBinding(@Autowired Queue deadQueue, @Autowired Exchange deadExchange) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING_KEY).noargs();
    }
}
